package handlers

import (
	"cloudgo/models"
	"html/template"
	"net/http"
	"strings"
)

const (
	// Public 代表服务器下将要提供静态文件的目录
	Public = "public/"
	// Static 接受静态文件请求的前缀，这里之所以定义常量是是方便前端查看、更改
	Static = "/static/"
)

// IndexHandler 访问首页，默认是登陆页
func IndexHandler(w http.ResponseWriter, r *http.Request) {
	http.ServeFile(w, r, Public+"html/login.html")
}

// UserHandler 访问用户页
func UserHandler(w http.ResponseWriter, r *http.Request) {
	// 需要拿到id
	ids := strings.TrimPrefix(r.URL.Path, "/user/")
	// 查找用户的model数据
	user, ok := models.Users[ids]
	if ok {
		t, _ := template.ParseFiles(Public + "html/info.html")
		t.Execute(w, user)
	} else {
		w.Write([]byte("No Such User!"))
	}
}
